package com.github.bitsapling.sapling.service.impl;

import com.github.bitsapling.sapling.enums.LoginType;
import com.github.bitsapling.sapling.mapper.LoginHistoryMapper;
import com.github.bitsapling.sapling.entity.LoginHistory;
import com.github.bitsapling.sapling.entity.User;
import com.github.bitsapling.sapling.service.LoginHistoryService;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;


@Service
public class LoginHistoryServiceImpl implements LoginHistoryService {
    @Resource
    private LoginHistoryMapper loginHistoryMapper;

    @Override
    public void create(@NotNull User user, @NotNull LoginType loginType, @NotNull String ip, @NotNull String userAgent) {
        LoginHistory history = new LoginHistory();
        history.setUserId(user.getId());
        history.setIpAddress(ip);
        history.setTime(LocalDateTime.now());
        history.setUserAgent(userAgent);
        history.setType(String.valueOf(loginType));
        history.setLocation("Unknown - GeoIP not initialized");
        loginHistoryMapper.insert(history);
    }
}
